﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TestWin
{
    public partial class SdForm : Form, See51.IGcSee51AvCall
    {
        private string m_strDevId = "c84439000001";
        private string m_strTurnIp = "39.108.213.89";
        long m_ListenerHandle = 0;
        Int64 m_nDevHandle = 0;

        //0000 0001： 1 文件      0000 0010：2   断开监听   0000 0100：  4 音频监听 
        //0000 1000:  8 视频监听  0001 0000：16  报警监听
        public int GetListenerType()
        {
            return 2 | 1;
        }
       

        public void OnSeeAv(String deviceId, See51.SFrameHead head, Byte[] data)
        {


        }
        public void OnAlarm(String deviceId, See51.SAlarmInfo alarm)
        {
           

        }
        //设备断开通知，此函数内不能实现关于设备任何的操作，仅仅作为通知
        public void OnClose(string deviceId)
        {

        }
        public void OnDownFileOver(string deviceId, string fileName)
        {

        }
        public long GetObjHandle()
        {
            return m_ListenerHandle;
        }
        public void SeetObjHandle(long handle)
        {
            m_ListenerHandle = handle;
        }
        public SdForm()
        {
            InitializeComponent();
        }

        public void SetIpInfo(string strDevId, string strTurnIp)
        {
            m_strDevId = strDevId;
            m_strTurnIp = strTurnIp;
        }
        public void GetSdRoot()
        {
            
            Int64 hh = See51.CSharpSee51Sdk.GetDevHandle(m_strDevId, m_strTurnIp);
            String rootdir = See51.CSharpSee51Sdk.See51SDKGetDevDir(hh);
            var arrdata = Newtonsoft.Json.Linq.JArray.Parse(rootdir);
            AppInfo.ListFileInfo.Clear();
            AppInfo.ListFileInfo = arrdata.ToObject<List<AppInfo.FileInfo>>();
            
            this.dataGridView1.DataSource = AppInfo.ListFileInfo;
            if (m_nDevHandle != 0)
            {
                
                int ii = See51.CSharpSee51Sdk.ReleaseDevHandle(hh);
                if (ii == 0) {
                    m_nDevHandle = 0;
                }
            }else {
                m_nDevHandle = hh;
            }
             
        }
        string G_Filename;
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int index = e.RowIndex;
            if (index < 0)
                return;
            AppInfo.FileInfo fInfo = AppInfo.ListFileInfo[index];
            DataGridViewColumn colu = dataGridView1.Columns[e.ColumnIndex];
            if (colu.Name == "FileDown")
            {
              
                //在线状态
                if (fInfo.isdir == "1")
                {
                    //2019 0529 11
                    G_Filename = fInfo.filename;
                    if (fInfo.filename.Length >= 10)
                    {
                        G_Filename = fInfo.filename.Substring(0, 8);
                        G_Filename = G_Filename + "/" + fInfo.filename;
                    }
                    String rootdir = See51.CSharpSee51Sdk.See51SDKGetDevFileList(m_nDevHandle, G_Filename);
                    var arrdata = Newtonsoft.Json.Linq.JArray.Parse(rootdir);
                    //  AppInfo.ListFileInfo.Clear();
                    AppInfo.ListFileInfo = arrdata.ToObject<List<AppInfo.FileInfo>>();
                    this.dataGridView1.DataSource = AppInfo.ListFileInfo;
                }else {
                    int filesize = Int32.Parse(fInfo.filesize);
                    if (filesize > 0)
                    {
                        string _Filename = fInfo.filename;
                        See51.CSharpSee51Sdk.See51SDKGetDownDevFile(m_nDevHandle, _Filename, filesize);
                        DownFileForm dor = new DownFileForm(m_nDevHandle, _Filename);
                        dor.ShowDialog();
                    }
                }
            }
        }
    }
}
